#!/usr/bin/python
from __future__ import with_statement  
from contextlib import closing  
import socket  
import multitask  
import string
  
def client_handler(sock): 
	global inrangecount
	global outrangecount
	global notfoundcount
	global overcount
	global cmdtable
	global rangetable
	with closing(sock):  
		while True:  
			data = (yield multitask.recv(sock, 1024))  
			if not data:  
				break 
			if data == 'reset':
				reset()
				yield multitask.send(sock, 'reset ok')
				break;
			if data == 'log':
				print 'inrange: '+str(inrangecount)
				print 'outrange: '+str(outrangecount)
				print 'notfoundcount: '+str(notfoundcount)
				print 'overcount: '+str(overcount)
				loginfo= 'inrange: '+str(inrangecount)+'\n'+'outrange: '+str(outrangecount)+'\n'+'notfoundcount: '+str(notfoundcount)+'\n'+'overcount: '+str(overcount)
				yield multitask.send(sock, loginfo)
				break;
			if data in cmdtable:
				print 'data received:'+data
				found=0
				for i in range(0,len(cmdtable[data])):
					if cmdtable[data][i][1]==0:
						found=1
						cmdtable[data][i]=(cmdtable[data][i][0],1)
						index=cmdtable[data][i][0]
						if rangetable[index]==0:
							print 'data index is '+str(index)+' but is not in range. return 0'
							outrangecount=outrangecount+1
							yield multitask.send(sock, '0')
							break
						else:
							inrangecount=inrangecount+1
							print 'data index is '+str(index)+' and is in range. return 1'
							yield multitask.send(sock, '1')
							break
				if found==0:
					overcount=overcount+1
					print 'no statement availabe found return 0'
					yield multitask.send(sock, '0')
					
			else:
				notfoundcount=notfoundcount+1
				print 'invalid data received:'+data+' return -1'
				yield multitask.send(sock, '-1')
		
			
  
def echo_server(hostname, port):  
	addrinfo = socket.getaddrinfo(hostname, port,  
								  socket.AF_INET,  
								  socket.SOCK_STREAM)  
	  
	(family, socketype, porot, canonname, sockaddr) = addrinfo[0]  
	with closing(socket.socket(family,  
							   socketype,  
							   porot)) as sock:  
		sock.setsockopt(socket.SOL_SOCKET,  
						socket.SO_REUSEADDR, 1)  
		sock.bind(sockaddr)  
		sock.listen(24)  
		while True:  
			multitask.add(client_handler((  
						yield multitask.accept(sock))[0]))  

def read_range(filename):
	result={}
	for i in range(0,2000):
		result[i]=0
	fd = file(filename,'r')
	start=0
	end=0
	while True:
		line=fd.readline()
		if len(line)==0:
			break;
		pos=line.index(' ')
		start=string.atoi(line[0:pos])
		end=string.atoi(line[pos+1:len(line)-1])
		for j in range(start,end):
			result[j]=1
	fd.close()
	return result

def read_all_cmds(filename):
	result={}
	f = file(filename,'r')
	number=0
	cmd=''
	count=0
	while True:
		line=f.readline()
		if len(line)==0:
			break;
		if count%2==0:
			number=string.atoi(line[0:len(line)-1])
		else:
			cmd=line[0:len(line)-1]
			if cmd in result:
				result[cmd].append((number,0))
			else:
				result[cmd]=[(number,0)]
		count=count+1
	f.close()
	return result

def reset():
	global inrangecount
	global outrangecount
	global notfoundcount
	global overcount
	global cmdtable
	global rangetable

	inrangecount=0
	outrangecount=0
	notfoundcount=0
	overcount=0
	print 'resetting server'
	cmdtable = read_all_cmds('/root/sunhaiyang/allcmds')
	rangetable = read_range('/root/range')
	print 'resetting server done'
						 
if __name__ == '__main__': 
	inrangecount=0
	outrangecount=0
	notfoundcount=0
	overcount=0

	cmdtable = read_all_cmds('/root/sunhaiyang/allcmds')
	rangetable = read_range('/root/range')
	import sys  
	hostname = 'localhost'  
	port = 8001  
	  
	if len(sys.argv) > 1:  
		hostname = sys.argv[1]  
	  
	if len(sys.argv) > 2:  
		port = int(sys.argv[2])  
	  
	multitask.add(echo_server(hostname, port))  
	  
	try:  
		multitask.run()  
	except KeyboardInterrupt:  
		pass  
